package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* MeView2
* */
class MeView2 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_FF6D00)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2

            save()
            translate(cx, cy)

            val mStrokeWidth = (cx * 1 / 12).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth

            val base0 = 7 / 12F
            val base1 = 11 / 12F

            save()
            translate(-cx * 1 / 6, 0F)
            val angle0 = 135.0
            val angle1 = 45.0
            drawArc(
                -cx * base0,
                -cy * base0,
                cx * base0,
                cy * base0,
                135F,
                270F,
                false,
                mPaint0
            )

            drawLine(
                cx * base0 * Math.cos(Math.toRadians(angle0)).toFloat(),
                cx * base0 * Math.sin(Math.toRadians(angle0)).toFloat(),
                cx * base1 * Math.cos(Math.toRadians(angle0)).toFloat(),
                cx * base1 * Math.sin(Math.toRadians(angle0)).toFloat(),
                mPaint0
            )

            drawLine(
                cx * base0 * Math.cos(Math.toRadians(angle1)).toFloat(),
                cx * base0 * Math.sin(Math.toRadians(angle1)).toFloat(),
                cx * base1 * Math.cos(Math.toRadians(angle1)).toFloat(),
                cx * base1 * Math.sin(Math.toRadians(angle1)).toFloat(),
                mPaint0
            )
            restore()

            save()
            translate(cx * 1 / 6, 0F)
            val angel2 = 45.0
            drawArc(
                -cx * base0,
                -cy * base0,
                cx * base0,
                cy * base0,
                315F,
                90F,
                false,
                mPaint0
            )

            drawLine(
                cx * base0 * Math.cos(Math.toRadians(angel2)).toFloat(),
                cx * base0 * Math.sin(Math.toRadians(angel2)).toFloat(),
                cx * base1 * Math.cos(Math.toRadians(angel2)).toFloat(),
                cx * base1 * Math.cos(Math.toRadians(angel2)).toFloat(),
                mPaint0
            )
            restore()

            restore()
        }
    }
}